package main.java.com.chenchangjie.servlet;

import main.java.com.chenchangjie.eneity.OrderItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/deleteOrderItem")
public class OrderItemDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pName = req.getParameter("pName");
        HttpSession session = req.getSession();
        List<OrderItem> list = (List<OrderItem>) session.getAttribute("ois");
        int index = 0;
        for(OrderItem o : list){
            if(pName.equals(o.getProduct().getName()))
                break;
            index++;
        }
        list.remove(index);
        session.setAttribute("ois",list);
        resp.sendRedirect("listOrderItem.jsp");
    }
}
